<?php

namespace App\Http\Controllers;

use App\Models\AlbumClass;
use Exception;
use Illuminate\Http\Request;

/**
 * 店铺相册相关
 */
class AlbumController extends Controller
{
    /**
     * 店铺相册首页
     * @param  Request $request [description]
     * @return [type]           [description]
     */
    public function shop_albums(Request $request)
    {
        try {
            $status  = 0;
            $data    = [];
            $shop_id = $request->get('shop_id');
            if (empty($shop_id)) {
                throw new Exception('缺少参数');
            }
            $albums = AlbumClass::where(['shop_id' => $shop_id, 'is_del' => 1])->where('type', '>', 0)->get();
            //处理数据
            foreach ($albums as $key => $value) {
                $data[$key]['ac_name']  = $value->ac_name; //相册名称
                $data[$key]['ac_cover'] = $value->ac_cover; //相册封面
                $data[$key]['type']     = $value->type; //type
                $pics                   = [];
                foreach ($value->album_pics as $k => $v) {
                    if (my_file_exists($v->ap_url) && strlen($v->ap_url) > 30) {
                        $pics[$k] = $v->ap_url;
                    }
                }
                $data[$key]['album_pics'] = $pics;
            }

            $status = 1;
            $msg    = '查询成功';
        } catch (Exception $e) {
            $msg = $e->getMessage();
        }

        return response()->json(['status' => $status, 'msg' => $msg, 'data' => $data]);
    }
}
